﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.IO;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Windows.Forms;

namespace TemplateH4.admin
{
    public partial class QLCSDL_Backup : System.Web.UI.Page
    {
        private static Server srvSql;
        protected void Page_Load(object sender, EventArgs e)

        {
            if (!IsPostBack)
            {
                txtfilebackup.Enabled = false;
                listfilerestore.Enabled = false;
                btnbackup.Enabled = false;
                btnrestore.Enabled = false;
                RequiredFieldValidator3.Enabled = false;
            }
            txtusername.Enabled = true;
            txtusername.Text = "sa";
            txtpassword.Enabled = true;
            txtpassword.Text = "123456";


        }

        protected void btnConnect_Click(object sender, EventArgs e)
        {
            // Create a new connection to the selected server name
            ServerConnection srvConn = new ServerConnection("localhost");
            // Log in using SQL authentication instead of Windows authentication
            srvConn.LoginSecure = false;
            // Give the login username
            srvConn.Login = txtusername.Text;
            // Give the login password
            srvConn.Password = txtpassword.Text;
            // Create a new SQL Server object using the connection we created
            srvSql = new Server(srvConn);
            // Loop through the databases list
            foreach (Database dbServer in srvSql.Databases)
            {
                // Add database to combobox
                listDB.Items.Add(dbServer.Name);
            }
            listDB.SelectedIndex = 0;
            txtfilebackup.Enabled = true;
            txtfilebackup.Text = listDB.SelectedItem.ToString();
            listfilerestore.Enabled = true;
            btnbackup.Enabled = true;
            btnrestore.Enabled = true;
            RequiredFieldValidator3.Enabled = true;

            DirectoryInfo resDir = new DirectoryInfo(Server.MapPath("~/admin/backup"));
            FileInfo[] fileinfo = resDir.GetFiles("*.bak");
            listfilerestore.Items.Clear();

            foreach (FileInfo temp in fileinfo)
            {
                listfilerestore.Items.Add(temp.Name);
                //listfilerestore.SelectedIndex = 0;

            }

                       

        }

        protected void btnbackup_Click(object sender, EventArgs e)
        {

            if (srvSql != null)
            {
                
                    // If the user has chosen a path where to save the backup file
                    // Create a new backup operation
                    Backup bkpDatabase = new Backup();
                    // Set the backup type to a database backup
                    bkpDatabase.Action = BackupActionType.Database;
                    // Set the database that we want to perform a backup on
                    bkpDatabase.Database = listDB.SelectedItem.ToString();

                    // Set the backup device to a file
                    BackupDeviceItem bkpDevice = new BackupDeviceItem(Server.MapPath("~/admin/backup/" + txtfilebackup.Text + ".bak"), DeviceType.File);
                    // Add the backup device to the backup
                    bkpDatabase.Devices.Add(bkpDevice);
                    // Perform the backup
                    bkpDatabase.SqlBackup(srvSql);
                    Response.Write("Backup xong");

            }
        }

        protected void btnrestore_Click(object sender, EventArgs e)
        {
            if (srvSql != null)
            {
                if (listfilerestore.SelectedIndex != -1)
                {

                    // If the user has chosen the file from which he wants the database to be restored

                    // Create a new database restore operation
                    Restore rstDatabase = new Restore();
                    // Set the restore type to a database restore
                    rstDatabase.Action = RestoreActionType.Database;
                    // Set the database that we want to perform the restore on
                    rstDatabase.Database = listDB.SelectedItem.ToString();

                    // Set the backup device from which we want to restore, to a file
                    BackupDeviceItem bkpDevice = new BackupDeviceItem(Server.MapPath("~/admin/backup/" + listfilerestore.SelectedItem.Text), DeviceType.File);
                    // Add the backup device to the restore type
                    rstDatabase.Devices.Add(bkpDevice);
                    // If the database already exists, replace it
                    rstDatabase.ReplaceDatabase = true;
                    // Perform the restore
                    rstDatabase.SqlRestore(srvSql);
                    Response.Write("Restore xong");
                }
            }
        }
    }
}
